# -*- mode: python; -*-
# windows reference: http://www.boost.org/doc/libs/1_62_0/more/getting_started/windows.html
import os
import sys
import shutil

boost_root = os.getenv('BOOST_ROOT')
if boost_root is None:
    print('BOOST_ROOT environment variable is not set')
    sys.exit(1)

env = DefaultEnvironment()

if env['OPT'] == 'debug':
    lib_file_flag = '-gd'
elif env['OPT'] == 'production':
    lib_file_flag = ''

libpath = Dir(boost_root).abspath
if not os.path.exists(libpath):
    print('%s not present' % libpath)
    sys.exit(1)

def copyboostlib(tofile, fromfile=None):
    if not os.path.exists(tofile):
        fromfile = Dir(boost_root).abspath + '/lib64-msvc-14.0/' + 'libboost_' + fromfile + '-vc140-mt' + lib_file_flag + '-1_62.lib'
        print('copying ' + fromfile + ' to ' + tofile)
        shutil.copyfile(fromfile, tofile)


def prepare_boost_libs(target, source, env):
    buildlibdir = Dir('#build/lib').abspath + '/'
    copyboostlib(buildlibdir + 'boost_program_options.lib', 'program_options')
    copyboostlib(buildlibdir + 'boost_regex.lib', 'regex')
    copyboostlib(buildlibdir + 'boost_system.lib', 'system')
    copyboostlib(buildlibdir + 'boost_thread.lib', 'thread')
    copyboostlib(buildlibdir + 'boost_filesystem.lib', 'filesystem')
    copyboostlib(buildlibdir + 'boost_chrono.lib', 'chrono')

    copyboostlib(buildlibdir + 'libboost_system-vc140-mt' + lib_file_flag + '-1_62.lib', 'system')
    copyboostlib(buildlibdir + 'libboost_date_time-vc140-mt' + lib_file_flag + '-1_62.lib', 'date_time')
    copyboostlib(buildlibdir + 'libboost_regex-vc140-mt' + lib_file_flag + '-1_62.lib', 'regex')
    copyboostlib(buildlibdir + 'libboost_program_options-vc140-mt' + lib_file_flag + '-1_62.lib', 'program_options')
    copyboostlib(buildlibdir + 'libboost_thread-vc140-mt' + lib_file_flag + '-1_62.lib', 'thread')
    copyboostlib(buildlibdir + 'libboost_filesystem-vc140-mt' + lib_file_flag + '-1_62.lib', 'filesystem')
    copyboostlib(buildlibdir + 'libboost_chrono-vc140-mt' + lib_file_flag + '-1_62.lib', 'chrono')


products = [File('#build/lib/boost_filesystem.lib')]
boost_command = env.Command(products, Dir(boost_root), prepare_boost_libs)
env.Alias('boostbuild', '#build/lib/boost_filesystem.lib')
